﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Convert.DataAccess;
using System.Configuration;
using Convert.LOTSmodel;
using Convert.CPIAMmodel;
using System.Data.OleDb;
using System.Windows;
namespace Convert.DBconverter
{
    class DeptsConverter
    {
        private static int BATCH_SAVE_SIZE = 5000;
        public static string LotsConnectionString; //= @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\LOTS\lots.mdb";//@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\LOTS\lots.mdb";//ConfigurationManager.ConnectionStrings["LOTStoCPIAMconvert.Properties.Settings.lotsConnectionString"].ConnectionString;
        public static string CpiamConnectionString; //= ConfigurationManager.ConnectionStrings["LOTStoCPIAMconvert.Properties.Settings.CPIAMPOSMaxRetailConnectionString"].ConnectionString;

        public static void ConvertDepts(string lotsConnectionString, string cpiamConnectionString)
        {
            LotsConnectionString = lotsConnectionString;
            CpiamConnectionString = cpiamConnectionString;
            DataTable DeptGroupsDT = DataTables.CreateDeptGroupsDT();
            OleDbConnection dbConn = new OleDbConnection(LotsConnectionString);
            try
            {
                dbConn.Open();
                using (OleDbCommand command = dbConn.CreateCommand())
                {
                    command.CommandText = string.Format(@"SELECT DepartmentID, DeptName FROM Department");
                    OleDbDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                            Department department = new Department();
                            int i = 0;
                            department.DepartmentID = reader.IsDBNull(i) ? 0 : reader.GetInt32(i);
                            department.DeptName = reader.IsDBNull(++i) ? null : reader.GetString(i);
                            SaveDepts(department, DeptGroupsDT);
                        if (DeptGroupsDT.Rows.Count % BATCH_SAVE_SIZE == 0)
                        {
                            SaveAllDataTables(CpiamConnectionString, DeptGroupsDT);
                        }
                    }
                    SaveAllDataTables(CpiamConnectionString, DeptGroupsDT);
                }
            }
            catch (Exception ex)
            {
                Logger.LogWarningAndError("Error in Convert Depts ", ex);
                MessageBox.Show("Error converting Depts: " + ex.Message + "\n\nStackTrace=" + ex.StackTrace);
            }
            finally
            {
                dbConn.Close();
            }

        }

        private static void SaveAllDataTables(String ConnectionString, DataTable DeptGroupsDT)
        {
            if (DeptGroupsDT.Rows.Count > 0)
                dbSaveBatch.SaveBatch(DeptGroupsDT, "DeptGroups", ConnectionString);
        }

        private static void SaveDepts(Department department, DataTable DeptGroupsDT)
        {

            DeptGroups deptGroups = new DeptGroups(department.DeptName, department.DepartmentID);
            DeptGroups.SaveDeptGroupsToDataTable(deptGroups, DeptGroupsDT);
            //DeptGroupsDT.Rows.Add(deptGroups.DeptGroupID, deptGroups.DeptGroupName, deptGroups.DeptGroupNo);

        }
    }
}
